package com.tinyideatech.admin.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.core.text.Convert;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.system.entity.SysRole;
import com.tinyideatech.system.entity.SysRoleMenu;
import com.tinyideatech.system.service.ISysRoleMenuService;
import com.tinyideatech.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysRoleMenuService roleMenuService;

    @GetMapping()
    public String roleList() {
        return "system/role/list";
    }

    @GetMapping("/add")
    public String roleAdd() {
        return "system/role/add";
    }

    @GetMapping("/edit/{id}")
    public String roleEdit(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        SysRole role = roleService.getById(id);
        role.setMenuIds(roleService.getMenuIds(role));
        map.addAttribute("role", role);
        return "system/role/edit";
    }

    @ResponseBody
    @PostMapping("/list")
    public PageResult list(SysRole role) {
        QueryWrapper<SysRole> qw = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(role.getRoleName())){
            qw.like("role_name", role.getRoleName());
        }
        IPage<SysRole> page = startPage();
        IPage<SysRole> roleList = roleService.page(page, qw);
        return page(roleList);
    }

    @ResponseBody
    @PostMapping("/save")
    public ApiResult save(
            SysRole sysRole
    ) {
        roleService.saveOrUpdate(sysRole);
        roleMenuService.removeRoleMenu(sysRole);
        if(sysRole.getMenuIds() != null && !sysRole.getMenuIds().equals("")){
            List<SysRoleMenu> rmList = roleService.setRoleMenuList(sysRole);
            if(rmList.size() > 0){
                roleMenuService.saveBatch(rmList);
            }
        }
        return success();
    }

    @ResponseBody
    @PostMapping("/remove")
    public ApiResult remove(
            String ids
    ){
        Long[] roleIds = Convert.toLongArray(ids);
        for (Long roleId : roleIds){
            SysRole role = roleService.getById(roleId);
            if (role != null && roleService.selectUserCountByRoleId(roleId) > 0) {
                return error("存在有该角色的用户！");
            }
        }
        roleService.removeByIds(Arrays.asList(roleIds));
        return success();
    }

    /**
     * 启用/停用
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    public ApiResult changeStatus(SysRole role) {
        roleService.changeStatus(role);
        return success();
    }

    /**
     * 校验角色名称
     */
    @PostMapping("/checkRoleNameUnique")
    @ResponseBody
    public ApiResult checkRoleNameUnique(SysRole role) {
        return success(roleService.checkRoleNameUnique(role));
    }

    /**
     * 校验角色权限
     */
    @PostMapping("/checkRoleKeyUnique")
    @ResponseBody
    public ApiResult checkRoleKeyUnique(SysRole role) {
        return success(roleService.checkRoleKeyUnique(role));
    }


}
